home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 5 / QRZ Ham Radio Callsign Database - Volume 5.iso / files / tcpip / amiga / asrc29p.lha / kiss.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-29  |  3.4 KB  |  173 lines

  1. #include "global.h"
  2. #include "mbuf.h"
  3. #include "amiga/asy.h"
  4. #include "iface.h"
  5. #include "kiss.h"
  6. #include "slip.h"
  7. #include "ax25.h"
  8.  
  9. int kiss_stop __ARGS((struct iface *iface));
  10.  
  11. /* Send raw data packet on KISS TNC */
  12. int
  13. kiss_raw(iface,data)
  14. struct iface *iface;
  15. struct mbuf *data;
  16. {
  17.     register struct mbuf *bp;
  18.  
  19.     /* Put type field for KISS TNC on front */
  20.     if((bp = pushdown(data,1)) == NULLBUF){
  21.         free_p(data);
  22.         return -1;
  23.     }
  24.     bp->data[0] = KISS_DATA;
  25.     bp->data[0] |= (iface->port << 4);
  26.  
  27.     iface->rawsndcnt++;
  28.     iface->lastsent = Clock;
  29.  
  30.     /* slip_raw also increments sndrawcnt */
  31.     slip_raw(Slip[iface->xdev].iface,bp);
  32.     return 0;
  33. }
  34.  
  35. /* Process incoming KISS TNC frame */
  36. void
  37. kiss_recv(iface,bp)
  38. struct iface *iface;
  39. struct mbuf *bp;
  40. {
  41.     char kisstype;
  42.     struct iface *kissif;
  43.     int port;
  44.  
  45.     kisstype = pullchar(&bp);
  46.     port = kisstype >> 4;
  47.  
  48.     if((kissif = Slip[iface->xdev].kiss[port]) == NULLIF){
  49.         free_p(bp);
  50.         return;
  51.     }
  52.  
  53.     switch(kisstype & 0xf){
  54.     case KISS_DATA:
  55.         ax_recv(kissif,bp);
  56.         break;
  57.     default:
  58.         free_p(bp);
  59.         break;
  60.     }
  61. }
  62.  
  63. /* Perform device control on KISS TNC by sending control messages */
  64. int
  65. kiss_ioctl(iface,argc,argv)
  66. struct iface *iface;
  67. int argc;
  68. char *argv[];
  69. {
  70.     struct mbuf *hbp;
  71.     int i;
  72.     char *cp;
  73.  
  74.     if(argc < 1){
  75.         tprintf("Data field missing\n");
  76.         return 1;
  77.     }
  78.     /* Allocate space for arg bytes */
  79.     if((hbp = ambufw((int16)argc)) == NULLBUF){
  80.         free_p(hbp);
  81.         return 0;
  82.     }
  83.     hbp->cnt = argc;
  84.     hbp->next = NULLBUF;
  85.     for(i=0,cp = hbp->data;i < argc;)
  86.         *cp++ = atoi(argv[i++]);
  87.  
  88.     if(hbp->data[0] != (char) KISS_RETURN)
  89.         hbp->data[0] |= (iface->port << 4);
  90.  
  91.     iface->rawsndcnt++;
  92.     iface->lastsent = Clock;
  93.  
  94.     slip_raw(Slip[iface->xdev].iface,hbp);    /* Even more "raw" than kiss_raw */
  95.     return 0;
  96. }
  97.  
  98. int
  99. kiss_stop(iface)
  100. struct iface *iface;
  101. {
  102.     Slip[iface->xdev].kiss[iface->port] = NULLIF;
  103.     return 0;
  104. }
  105.  
  106. /* Attach a kiss interface to an existing asy interface in the system
  107.  * argv[0]: hardware type, must be "kiss"
  108.  * argv[1]: master interface, e.g., "ax4"
  109.  * argv[2]: kiss port, e.g., "4"
  110.  * argv[3]: interface label, e.g., "ax0"
  111.  * argv[4]: maximum transmission unit, bytes
  112.  */
  113. int
  114. kiss_attach(argc,argv,p)
  115. int argc;
  116. char *argv[];
  117. void *p;
  118. {
  119.     struct iface *if_asy, *if_kiss;
  120.     int port;
  121.  
  122.     if((if_asy = if_lookup(argv[1])) == NULLIF){
  123.         tprintf("Interface %s does not exist\n",argv[1]);
  124.         return -1;
  125.     }
  126.  
  127.     if(if_lookup(argv[3]) != NULLIF){
  128.         tprintf("Interface %s already exists\n",argv[4]);
  129.         return -1;
  130.     }
  131.  
  132.     if((port = atoi(argv[2])) == 0){
  133.         tprintf("Port 0 automatically assigned to interface %s\n",argv[1]);
  134.         return -1;
  135.     }
  136.  
  137.     if(port < 1 || port > 15){
  138.         tprintf("Ports 1 to 15 only\n");
  139.         return -1;
  140.     }
  141.  
  142.     if(Slip[if_asy->xdev].kiss[port] != NULLIF){
  143.         tprintf("Port %d already allocated on interface %s\n", port, argv[1]);
  144.         return -1;
  145.     }
  146.     /* Create interface structure and fill in details */
  147.     if_kiss = (struct iface *)callocw(1,sizeof(struct iface));
  148.     if_kiss->addr = if_asy->addr;
  149.     if_kiss->name = strdup(argv[3]);
  150.  
  151.     if(argc >= 5){
  152.         if_kiss->mtu = atoi(argv[4]);
  153.     } else {
  154.         if_kiss->mtu = if_asy->mtu;
  155.     }
  156.  
  157.     if_kiss->dev = if_asy->dev;
  158.     if_kiss->stop = kiss_stop;
  159.     setencap(if_kiss,"AX25");
  160.     if_kiss->ioctl = kiss_ioctl;
  161.     if_kiss->raw = kiss_raw;
  162.     if(if_kiss->hwaddr == NULLCHAR)
  163.         if_kiss->hwaddr = mallocw(AXALEN);
  164.     memcpy(if_kiss->hwaddr,Mycall,AXALEN);
  165.     if_kiss->xdev = if_asy->xdev;
  166.     if_kiss->next = Ifaces;
  167.     Ifaces = if_kiss;
  168.     if_kiss->port = port;
  169.     Slip[if_kiss->xdev].kiss[if_kiss->port] = if_kiss;
  170.     return 0;
  171. }
  172.  
  173.